package com.hlhj.carview.aty

import android.content.Intent
import android.widget.RadioButton
import com.bumptech.glide.Glide
import com.example.mymvp.okrx.BaseBean
import com.hlhj.carview.BaseAty
import com.hlhj.carview.Iview.AddCarView
import com.hlhj.carview.Iview.UploadView
import com.hlhj.carview.R
import com.hlhj.carview.Toast
import com.hlhj.carview.bean.MyBaseBean
import com.hlhj.carview.http.CustomAction
import com.hlhj.carview.http.MyJsonConvert
import com.hlhj.carview.http.Urls
import com.hlhj.carview.presenter.AddCarPresenter
import com.hlhj.carview.presenter.UploadPresenter
import com.hlhj.carview.utils.MyGlideEngin
import com.hlhj.carview.utils.ToastUtil
import com.huantansheng.easyphotos.EasyPhotos
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import com.lzy.okrx2.adapter.ObservableResponse
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.aty_addcar.*
import java.io.File

/**
 * Created by Never Fear   on 2018\12\21 0021.
Never More....
 */
class AddCarAty :BaseAty<AddCarView,AddCarPresenter>(), AddCarView, UploadView {


    private var numType=1
    private var fullUrl:String?=null
    private var imgUrl1:String?=null
    private var imgUrl2:String?=null
    private var uploadPresenter: UploadPresenter?=null

    override fun setAddCarResult(myBaseBean: MyBaseBean) {
        ToastUtil.toast("添加成功",this)
        finish()
    }
    override fun setUpResult(myBaseBean: MyBaseBean) {
    }

    override fun setUpResult(myBaseBean: MyBaseBean, tag: Int) {
        when(tag){
            TAKEPHOTO0->{
                fullUrl=myBaseBean.info.toString()
            }
            TAKEPHOTO1->{
                imgUrl1=myBaseBean.info.toString()

            }
            TAKEPHOTO2->{
                imgUrl2=myBaseBean.info.toString()
            }
        }

    }
    override fun initView() {
        val button = rgType.getChildAt(0) as RadioButton
        button.isChecked=true
    }

    override fun logicStart() {
    }

    private val TAKEPHOTO0: Int=1000
    private val TAKEPHOTO1: Int=1001
    private val TAKEPHOTO2: Int=1002

    override fun setListener() {
        rgType.setOnCheckedChangeListener { group, checkedId ->
            when(checkedId){
                R.id.rbBlue->{
                    numType=1
                }
                R.id.rbGreen->{
                    numType=2
                }
                R.id.rbYellow->{
                    numType=3
                }
            }
        }
        btCommit.setOnClickListener {
            if (etCarnum.text.isEmpty()){
                ToastUtil.toast("请填写车牌号码",this)
                return@setOnClickListener
            }
            if (etCarType.text.isEmpty()){
                ToastUtil.toast("请填写车型",this)
                return@setOnClickListener
            }
            OkGo.post<MyBaseBean>(Urls.PubUrl)
                    .headers("type","1")
                    .params("uriPath","addCar")
                    .converter(object : MyJsonConvert<MyBaseBean>(){})
                    .adapt(ObservableResponse<MyBaseBean>())
                    .subscribeOn(Schedulers.io())
                    .subscribe(object :CustomAction<Response<MyBaseBean>>(this){
                        override fun httpSuccess(t: Response<MyBaseBean>) {
                            "提交成功，等待审核".Toast()
                            finish()
                        }
                    })
        }
        img0.setOnClickListener {
            EasyPhotos.createAlbum(this, true, MyGlideEngin())//参数说明：上下文，是否显示相机按钮，[配置Glide为图片加载引擎](https://github.com/HuanTanSheng/EasyPhotos/wiki/12-%E9%85%8D%E7%BD%AEImageEngine%EF%BC%8C%E6%94%AF%E6%8C%81%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD%E5%BA%93)
                    .setFileProviderAuthority("hlhj.fhp.carview.photo")//参数说明：见下方`FileProvider的配置`
                    .start(TAKEPHOTO0)
        }

        img1.setOnClickListener {
            EasyPhotos.createAlbum(this, true, MyGlideEngin())//参数说明：上下文，是否显示相机按钮，[配置Glide为图片加载引擎](https://github.com/HuanTanSheng/EasyPhotos/wiki/12-%E9%85%8D%E7%BD%AEImageEngine%EF%BC%8C%E6%94%AF%E6%8C%81%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD%E5%BA%93)
                    .setFileProviderAuthority("hlhj.fhp.carview.photo")//参数说明：见下方`FileProvider的配置`
                    .start(TAKEPHOTO1)
        }

        img2.setOnClickListener {
            EasyPhotos.createAlbum(this, true, MyGlideEngin())//参数说明：上下文，是否显示相机按钮，[配置Glide为图片加载引擎](https://github.com/HuanTanSheng/EasyPhotos/wiki/12-%E9%85%8D%E7%BD%AEImageEngine%EF%BC%8C%E6%94%AF%E6%8C%81%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD%E5%BA%93)
                    .setFileProviderAuthority("hlhj.fhp.carview.photo")//参数说明：见下方`FileProvider的配置`
                    .start(TAKEPHOTO2)
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode!=-1)return
        val pathList = data?.getStringArrayListExtra(EasyPhotos.RESULT_PATHS)!!
        when(requestCode){
            TAKEPHOTO0->{
                uploadPresenter?.upload(File(pathList[0]),this,TAKEPHOTO0)
                Glide.with(this).load(File(pathList[0])).into(img0)
            }
            TAKEPHOTO1->{
                uploadPresenter?.upload(File(pathList[0]),this,TAKEPHOTO1)
                Glide.with(this).load(File(pathList[0])).into(img1)
            }
            TAKEPHOTO2->{
                uploadPresenter?.upload(File(pathList[0]),this,TAKEPHOTO2)
                Glide.with(this).load(File(pathList[0])).into(img2)

            }
        }
    }
    override fun bindView(): AddCarView {
        return this
    }

    override fun createPresenter(): AddCarPresenter {
        uploadPresenter= UploadPresenter()
        uploadPresenter?.let {
            it.createModule()
            it.bindView(this@AddCarAty)
        }
        return AddCarPresenter()
    }

    override fun getContentID(): Int {
        return R.layout.aty_addcar
    }
}